package io;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;

/**
 * 对象流
 * java.io.ObjectInputStream和ObjectOutputStream
 * 对象流是一对高级流，作用是进行对象序列化与反序列化
 */
public class OOSDemo {
    public static void main(String[] args) throws IOException {
        //将Person对象写入文件person.obj
        String name = "王克晶";
        int age = 18;
        String gender = "女";
        String[] otherInfo = {"嗓门大", "黑", "技术好", "声音抑扬顿挫", "你们的启蒙老师"};
        Person person = new Person(name, age, gender, otherInfo);

        //低级流:文件流，用于将字节写入文件
        FileOutputStream fos = new FileOutputStream("person.obj");
        //高级流:对象流，用于将对象进行序列化
        ObjectOutputStream oos = new ObjectOutputStream(fos);
        /*
            对象输出流在进行序列化时要求该对象必须实现可序列化接口:
            java.io.Serializable
            否则在调用writeObject方法时抛出下列异常:
            java.io.NotSerializableException
         */
        oos.writeObject(person);
        System.out.println("写出完毕!");
        oos.close();
    }
}



